package com.itboye.app.mian.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.EnumUtility;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.adapter.NewsDongTaiAdapter;
import com.itboye.jmsfhz.app.adapter.PersonnelAdapter;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.data.model.HomePageModel;
import com.itboye.jmsfhz.data.model.LunBoModel;
import com.itboye.jmsfhz.data.presenter.LunBoPresenter;
import com.itboye.jmsfhz.data.presenter.Personnelpresenter;
import com.itboye.jmsfhz.widget.CarouselView;
import com.itboye.jmsfhz.widget.XListView;
import com.itboye.jmsfhz.widget.XListView.IXListViewListener;
import com.lfh.commentdemo.bean.AppModelNewsBean;

public class HeZuoChengGuoFragment extends BaseFragment implements Observer ,IXListViewListener{
	private List<ImageView> bmps;
	CarouselView topLunbo;
	LunBoPresenter lunBoPresenter;
	XListView mylistviewpernnel;
	TextView tvNull;
	LunBoModel Beannermodel;
	View view;
	NewsDongTaiAdapter adapter;
	Personnelpresenter personnelpresenter;
	AppModelNewsBean bean;
	int page_num=1;
	int page_size=10;
	String uid;
	@Override
	public int initView() {
		return R.layout.fragment_personnel;
	}

	@Override
	public void onMyActivityCreated() {
		showProgressDialog("数据加载中，请稍后...", true);
		Beannermodel = new LunBoModel();
		lunBoPresenter = new LunBoPresenter(this);
		Beannermodel.setPage_num(page_num + "");
		Beannermodel.setPage_size(page_size + "");
		Beannermodel.setPosition(6045);
		lunBoPresenter.getInfo(Beannermodel);
		
		view = LayoutInflater.from(getActivity()).inflate(R.layout.item_head,
				null);
		
		mylistviewpernnel.addHeaderView(view);
		topLunbo = (CarouselView) getView().findViewById(R.id.topLunbo);
	
		
		bean=new AppModelNewsBean();
		personnelpresenter=new Personnelpresenter(this);
		bean.setPage_num(page_num+"");
		bean.setPage_size(page_size+"");
		bean.setCate("6103");
		bean.setUid(uid);
		personnelpresenter.getInfo(bean);
		

		// ////////
		mylistviewpernnel.setXListViewListener(this);
		/** 刷新后获取数据 **/
		mylistviewpernnel.setPullLoadEnable(true);
		/** 刷新 **/
		mylistviewpernnel.setPullRefreshEnable(true);
		mylistviewpernnel.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "合作成果");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(arg2-2).getId());
				intent.putExtra("cate", bean.getCate());
				intent.putExtra("id", bean.getList().get(arg2-2).getId());
				bean.setCate(EnumUtility.COOPERATION.getValue() + "");
				startActivity(intent);
			}
		});

	}

	int position;

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);

			
			if(model.getEventType()==personnelpresenter.HeZuoSuccessEvent){
				bean=(AppModelNewsBean) model.getData();
				adapter=new NewsDongTaiAdapter(getActivity(),bean);
				mylistviewpernnel.setAdapter(adapter);
			
				}
			if (model.getEventType() == lunBoPresenter.BannerSuccessEvent) {
				Beannermodel = (LunBoModel) model.getData();
				bmps = new ArrayList<ImageView>();
				for (int i = 0; i < Beannermodel.getResult().size(); i++) {
					position = i;
					ImageView imageView = new ImageView(getActivity());

					imageView.setScaleType(ScaleType.FIT_XY);
					com.nostra13.universalimageloader.core.ImageLoader
							.getInstance().displayImage(
									GlobalConfig.JIUYE
											+ Beannermodel.getResult().get(i)
													.getImg(), imageView);
//					imageView.setOnClickListener(new OnClickListener() {
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//
//							if (Beannermodel.getResult().get(0).getUrl() == null
//									|| Beannermodel.getResult().get(0).getUrl() == "") {
//							} else {
//								Intent intent = new Intent(getActivity(),
//										WebActivity.class);
//								intent.putExtra("url", GlobalConfig.WEBURL
//										+ Beannermodel.getResult().get(0).getUrl());
//								startActivity(intent);
//							}
//
//						}
//					});
					bmps.add(imageView);

				}

				topLunbo.setImageBitmaps(bmps);
			}
//		}
			try {
				/**
				 * 当数据加载出来后dialog则消失
				 */
				closeProgressDialog();
			} catch (Exception e) {
			}

			stopLoad();
	}
	
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_num = 1;
		page_size = 10;
//		 bean.setPage_num(page_num + "");
		bean.setCate("6103");
		 bean.setPage_size(page_size + "");
		personnelpresenter.getInfo(bean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_num++;
		page_size++;
		bean.setCate("6103");
		// bean.setPage_num(page_num + "");
		 bean.setPage_size(page_size + "");
		personnelpresenter.getInfo(bean);
	}

	public void stopLoad() {
		mylistviewpernnel.stopLoadMore();
		mylistviewpernnel.stopRefresh();
	}

}
